﻿package com.d4p.util
{
	public class Percent 
	{
		
		private var fixedPoint:Number	
		private var _percent:Number
		private var _value:Number 	
		private var _minValue:Number = new Number(0);
		private var _maxValue:Number = new Number(100);
		private var length:Number
		
		public function Percent(percent:Number)
		{
			this.percent = percent;
			this.percentToValue();
		}
		
		private function toFixed(value:Number):Number
		{
			var s:String = value.toFixed(this.fixedPoint);
			return Number(s);
		}
		
		private function synchronize():void
		{
			// 최소값 , 최대값이 변경 되었을때 --> 렝스가 변함
			// 퍼센트를 기준으로 value값을 변경 시킨다.
			this.percentToValue();
		}

		
		private function getLength(min:Number, max:Number):Number
		{
			return 	Math.abs(max - min);
		}
		
		private function valueToPercent():void
		{
			this.length  = this.getLength(this.minValue, this.maxValue);
			var percent:Number= Operator.valueToPercent(this.value - this.minValue, this.length); 
			this._percent = (!this.fixedPoint) ? percent : this.toFixed(percent);
			
		}
		
		private function percentToValue():void{
			this.length  = this.getLength(this.minValue, this.maxValue);
			var value:Number = Operator.percentToValue(this.percent , this.length) + this.minValue
			this._value = (! this.fixedPoint) ?  value : this.toFixed(value);
			
		}
		
		public function set percent(percent:Number):void
		{
			this._percent = percent;
			this.percentToValue();
		}
		
		public function set value(value:Number):void
		{
			//// min <= value <= max
			//// 현재값 변경시 퍼센트 값 변경
			
			if(value < this.minValue || value > this.maxValue){
				throw new Error("error : min <= value <= max")
				return;
			}
			this._value = value;
			this.valueToPercent();
			
		}
		
		public function set minValue(min:Number):void
		{
			//// min < max
			if(min > this.maxValue){
				throw new Error("error : min > max")
				return;
			}
			this._minValue = min;
			this.synchronize();
		}
		
		public function set maxValue(max:Number):void
		{
			if(max < this.minValue){
				throw new Error("error : max < min");
				return;
			}
			this._maxValue = max	
			this.synchronize();
		}
		////////////////
		public function get percent():Number
		{
			return this._percent;
		}
		
		public function get minValue():Number
		{
			return this._minValue;
		}
		
		public function get value():Number
		{
			return this._value;
		}
		
		public function get maxValue():Number
		{
			return this._maxValue;
		}
		
		public function setFixedPoint(fp:uint):void
		{
			this.fixedPoint = fp
		}
		
		public function getFixedPoint():uint
		{
			return this.fixedPoint;
		}

		 public  function toString():String
		{
			var fix:Function = function(value:Number):String{
				return value.toFixed(fixedPoint);
			}
			var msg:String = new String();
			msg += "[";
			msg += "percent : "	+ fix(this.percent)  	+"\t";
			msg += "value : "	+ fix(this.value)		+"\t";
			msg += "min : "		+ this.minValue			+"\t";
			msg += "max : "		+ this.maxValue		 	+"\t";
			msg += "length : "	+ this.length			+"\t";
			msg += "fixedPoint : "	+ this.fixedPoint	+"\t";
			msg += "]";
			
			return msg;
		}
	}
}